package com.community.itelligentcommunity.sys.controller;

import com.community.itelligentcommunity.common.base.constant.SystemStaticConst;
import com.community.itelligentcommunity.common.util.user.UserInfo;
import com.community.itelligentcommunity.sys.entity.Tree;
import com.community.itelligentcommunity.sys.entity.User;
import com.community.itelligentcommunity.sys.service.UserService;
import com.community.itelligentcommunity.sys.vo.RegPassVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述：main主入口方法
 */
@Controller
public class MainController {

    @Autowired
    private UserService userService;

    /**
     * 功能描述：修改用户信息
     * @param entity
     * @return
     */
    @RequestMapping(value="/regUserInfo",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> regUserInfo(User entity){
        return userService.regUserInfo(entity);
    }

    /**
     * 功能描述：获取当前登陆的用户的信息
     * @return
     */
    @RequestMapping(value="/getLoginUser",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> getLoginUser(){
        Map<String,Object> result = new HashMap<String,Object>();
        User user = UserInfo.getUser();
        if(user==null){
            result.put(SystemStaticConst.MSG, "用户未登陆");
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            return result;
        }
        user = userService.get(user);
        result.put(SystemStaticConst.MSG, "获取用户信息成功");
        result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
        result.put("nickName",user.getNickName());
        return result;
    }

    /**
     * 功能描述：调整到登陆成功的主页
     * @param model
     * @return
     */
    @RequestMapping(value="/main",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public String main(Model model){
        List<Tree> treeList = userService.mainTree();
        model.addAttribute("treeList",treeList);
        return "main";
    }

    /**
     * 功能描述：实现修改密码
     * @return
     */
    @RequestMapping(value="/regPass",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> regPass(RegPassVo entity){
        return userService.regPass(entity);
    }


}
